package ldkxingzhe.ldkxingzhedemo.view.recyclerview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import ldkxingzhe.ldkxingzhedemo.R;

/**
 * Created by xingzhe on 15/11/15.
 */
public class RecyclerViewFirstActivity extends AppCompatActivity{
    private final String TAG = RecyclerViewFirstActivity.class.getSimpleName();

    RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview_first);
        recyclerView = (RecyclerView)findViewById(R.id.recycle_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new MyRecyclerViewAdapter());
    }

    private class MyRecyclerViewAdapter extends RecyclerView.Adapter{
        private final String TAG = MyRecyclerViewAdapter.class.getSimpleName();

        private String[] nameArray = {"唐辉","李鹏元","陆尚红","马武超","黄吉民","黄增薪","梁登科","胡宇","唐辉","李鹏元","陆尚红","马武超","黄吉民","黄增薪","梁登科","胡宇","孙嘉宾"};
        private String[] provinceArray = {"四川","河南","江苏","陕西","河南","河南","河南","湖南","四川","河南","江苏","陕西","河南","河南","河南","湖南","河南"};

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(RecyclerViewFirstActivity.this).inflate(R.layout.recyclerview_item_1, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            Log.v(TAG, "onBindViewHolder");
            MyViewHolder myViewHolder = (MyViewHolder)holder;
            myViewHolder.name.setText(nameArray[position]);
            myViewHolder.province.setText(provinceArray[position]);
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v(TAG,"onClick, and postion is " + position);
                    v.setBackgroundColor(Color.GREEN);
                }
            });
        }

        @Override
        public int getItemCount() {
            Log.v(TAG,"getItemCount, and count is " + nameArray.length);
            return nameArray.length;
        }

        private class MyViewHolder extends RecyclerView.ViewHolder{
            private final String TAG = MyViewHolder.class.getSimpleName();
            private TextView name, province;
            public MyViewHolder(View itemView) {
                super(itemView);
                name = (TextView)itemView.findViewById(R.id.name);
                province = (TextView)itemView.findViewById(R.id.province);
                Log.v(TAG, "In the constructor of MyViewHolder");
            }
        }
    }
}
